package com.wy.cms.controller;

import com.wy.cms.common.BaseController;
import com.wy.common.exception.BizException;
import com.wy.constant.SpringMVCConst;
import com.wy.dao.ex.IAreaCountyExDAO;
import com.wy.domain.AreaCounty;
import com.wy.domain.Works;
import com.wy.protocol.ERRORCODE;
import com.wy.protocol.PageQuery;
import com.wy.service.IAreaCountyService;
import com.wy.service.IWorksService;
import com.wy.service.ex.IAreaCountyExService;
import com.wy.service.ex.IWorksExService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@Scope(SpringMVCConst.SCOPE)
@RequestMapping(value="/areaCounty")
public class AreaCountyController extends BaseController {

    private static final Logger LOGGER= LoggerFactory.getLogger(AreaCountyController.class);

    @Autowired
    private IAreaCountyService areaCountyService;

    @Autowired
    private IAreaCountyExService areaCountyExService;

    /**
     * 分页查询
     * @return
     */
    @RequestMapping(value = "/findPage", method = RequestMethod.GET)
    @ResponseBody
    public List<AreaCounty> findPage(PageQuery pageQuery) throws BizException {

        if(pageQuery==null){
            pageQuery = new PageQuery();
        }

        List<AreaCounty> lists = null;

        try {

            lists = areaCountyExService.findPage(pageQuery);

        }catch(Exception e){
            throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
        }

        return lists;

    }

    /**
     * 数据总数
     * @return
     */
    @RequestMapping(value = "/findCount", method = RequestMethod.GET)
    @ResponseBody
    public int findCount() throws BizException {
        int count = 0;
        try {
            count = areaCountyExService.findCount();
        }catch(Exception e){
            throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
        }
        return count;
    }

    /**
     * 新增
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public boolean insert(AreaCounty areaCounty) throws BizException {
        if(areaCounty==null){
            throw new BizException(ERRORCODE.PARAM_ERROR.getCode(), ERRORCODE.PARAM_ERROR.getMessage());
        }
        boolean flag = false;
        try{
            areaCountyService.insert(areaCounty);
            flag = true;
        }catch(Exception e){
            throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
        }
        return flag;
    }

    /**
     * 根据ID修改
     * @return
     */
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    @ResponseBody
    public boolean updateById(AreaCounty areaCounty) throws BizException {
        if(areaCounty==null||areaCounty.getId()==null){
            throw new BizException(ERRORCODE.PARAM_ERROR.getCode(), ERRORCODE.PARAM_ERROR.getMessage());
        }
        boolean flag = false;
        try{
            areaCountyService.update(areaCounty);
            flag = true;
        }catch(Exception e){
            throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
        }
        return flag;
    }

    /**
     * 根据ID删除
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ResponseBody
    public boolean deleteById(@RequestParam(value="id",required=false) Long id) throws BizException {
        if(id==null||id<=0){
            throw new BizException(ERRORCODE.PARAM_ERROR.getCode(), ERRORCODE.PARAM_ERROR.getMessage());
        }
        boolean flag = false;
        try{
            areaCountyService.deleteById(id);
            flag = true;
        }catch(Exception e){
            throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
        }
        return flag;
    }

}

